/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

FIVe3D
Flash Interactive Vector-based 3D
Mathieu Badimon  |  five3d@mathieu-badimon.com

http://five3D.mathieu-badimon.com  |  http://five3d.mathieu-badimon.com/archives/  |  http://code.google.com/p/five3d/

/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import net.badimon.five3D.display.DisplayObjectContainer;
import net.badimon.five3D.display.DynamicText;
import net.badimon.five3D.display.Motif;
import net.badimon.five3D.display.Scene;
import net.badimon.five3D.geom.Angle;

dynamic class net.badimon.five3D.display.Sprite extends DisplayObjectContainer {

	private var __motif:Array;
	private var __renderedmotif:Array;

	function Sprite(scene:Scene, parentclip:MovieClip, name:String, depth:Number, parent:DisplayObjectContainer) {
		super(scene, parentclip, name, depth, parent);
		__motif = [];
	}

	public function createSprite(name:String, depth:Number):Sprite {
		if (this[name] == undefined) {
			getInstanceAtDepth(depth).removeSprite();
			return this[name] = __children[name] = new Sprite(__scene, _clip, name, depth, this);
		}
		else if (__children[name] != undefined) return __children[name];
	}

	public function getInstanceAtDepth(depth:Number):Sprite {
		for (var i:String in __children) if (__children[i].getDepth() == depth) return __children[i];
		return undefined;
	}

	public function removeSprite(Void):Void {
		delete _parent[_name];
		delete _parent.__children[_name];
		_clip.removeMovieClip();
	}

	public function createDynamicText(name:String, depth:Number):DynamicText {
		if (this[name] == undefined) {
			getInstanceAtDepth(depth).removeSprite();
			return this[name] = __children[name] = new DynamicText(__scene, _clip, name, depth, this);
		}
		else if (__children[name] != undefined) return __children[name];
		return null;
	}

	public function beginFill(color:Number, alpha:Number):Void {
		__motif.push(['B', [color, (alpha == undefined) ? 100 : alpha]]);
		__scene.__render = true;
		__render = true;
	}

	public function lineStyle(thickness:Number, color:Number, alpha:Number):Void {
		__motif.push(['S', [thickness, (color == undefined) ? 0x000000 : color, (alpha == undefined) ? 100 : alpha]]);
		__scene.__render = true;
		__render = true;
	}

	public function moveTo(x:Number, y:Number):Void {
		__motif.push(['M', [x, y]]);
		__scene.__render = true;
		__render = true;
	}

	public function lineTo(x:Number, y:Number):Void {
		__motif.push(['L', [x, y]]);
		__scene.__render = true;
		__render = true;
	}

	public function curveTo(controlx:Number, controly:Number, anchorx:Number, anchory:Number):Void {
		__motif.push(['C', [controlx, controly, anchorx, anchory]]);
		__scene.__render = true;
		__render = true;
	}

	public function endFill(Void):Void {
		__motif.push(['E']);
		__scene.__render = true;
		__render = true;
	}

	public function clear(Void):Void {
		__motif = [];
		__scene.__render = true;
		__render = true;
	}

	public function addMotif(motif:Array):Void {
		__motif = __motif.concat(motif);
		__scene.__render = true;
		__render = true;
	}

	public function get useHandCursor():Boolean {
		return _clip.useHandCursor;
	}

	public function set useHandCursor(value:Boolean):Void {
		_clip.useHandCursor = value;
	}

	public function get trackAsMenu():Boolean {
		return _clip.trackAsMenu;
	}

	public function set trackAsMenu(value:Boolean):Void {
		_clip.trackAsMenu = value;
	}

	public function render(Void):Void {
		if (!__visible && _clip._visible) _clip._visible = false;
		else if (__visible) {
			if (!_clip._visible) _clip._visible = true;
			if (__render) {
				getMatrices();
				_clip.clear();
				__renderedmotif = Motif.clone(__motif);
				Motif.project(__renderedmotif, __concatenatedmatrix, __scene._viewdistance);
				Motif.draw(_clip, __renderedmotif);
				__render = false;
			}
			for (var i:String in __children) __children[i].render();
		}
	}

	private function getMatrices(Void):Void {
		__matrix.createBox(__scalex / 100, __scaley / 100, __scalez / 100, Angle.degreesToRadians(__rotationx), Angle.degreesToRadians(__rotationy), Angle.degreesToRadians(__rotationz), __x, __y, __z);
		if (_parent == undefined) {
			__concatenatedmatrix = __matrix.clone();
		} else {
			__concatenatedmatrix = _parent.__concatenatedmatrix.clone();
			__concatenatedmatrix.concat(__matrix);
		}
	}
}